home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Commun⁄Network / RevRdist Folder / RevRdist / RevRdist src / AppleEvents.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-17  |  5.5 KB  |  233 lines  |  [TEXT/KAHL]

  1. /*
  2.  * AppleEvents.c - handle Apple Event -- what a lot of work
  3.  *
  4.  * Most of this code is straight out of IM VI
  5.  */
  6.  
  7. #include "RevRdist.h"
  8. #include "AppleEvents.h"
  9.  
  10. pascal OSErr myAEHandler (AppleEvent *ae, AppleEvent *reply, Longint refcon);
  11. OSErr MyGotRequiredParams(AppleEvent *ae);
  12. OSErr MyOpenFile(FSSpec *fss);
  13. OSErr DoHighLevelEvent (EventRecord *theEvent);
  14. OSErr init_AppleEvent(Boolean);
  15.  
  16. void doFileMenu (Integer);
  17.  
  18.  
  19. /*
  20.  *=========================================================================
  21.  * init_AppleEvent(flag) - register/unregister handlers
  22.  * entry:    flag = true to register handlers, false to remove them
  23.  *            (quit handler is not removed once installed)
  24.  *=========================================================================
  25.  */
  26. OSErr
  27. init_AppleEvent(Boolean flag)
  28. {
  29.     OSErr            error;
  30.  
  31.     if (flag)
  32.     {
  33.         error = AEInstallEventHandler (kCoreEventClass, kAEOpenApplication,
  34.             myAEHandler, kAEOpenApplication, false);
  35.         if (error == 0)
  36.             error = AEInstallEventHandler (kCoreEventClass, kAEOpenDocuments,
  37.                 myAEHandler, kAEOpenDocuments, false);
  38.         if (error == 0)
  39.             error = AEInstallEventHandler (kCoreEventClass, kAEQuitApplication,
  40.                 myAEHandler, kAEQuitApplication, false);
  41.     }
  42.     else
  43.     {
  44.         error = AERemoveEventHandler (kCoreEventClass, kAEOpenApplication,
  45.             myAEHandler, false);
  46.         if (error == 0)
  47.             error = AERemoveEventHandler (kCoreEventClass, kAEOpenDocuments,
  48.                 myAEHandler, false);
  49.     }
  50.     return error;
  51. }
  52.  
  53.  
  54.  
  55. /*
  56.  *=========================================================================
  57.  * myAEHandler(ae, reply, refcon) - handle Apple Event
  58.  * entry:    ae = ptr to event
  59.  *            reply = ptr to reply
  60.  *            refcon = event type
  61.  * returns:    OSErr
  62.  *=========================================================================
  63.  */
  64. pascal
  65. OSErr
  66. myAEHandler (AppleEvent *ae, AppleEvent *reply, Longint refcon)
  67. {
  68.     OSErr        error;
  69.     FSSpec        myFSS;
  70.     AEDescList    docList;
  71.     Longint        index, itemsInList;
  72.     Size        actualSize;
  73.     AEKeyword    keywd;
  74.     DescType    returnedType;
  75.  
  76.     Clue1 = "\pmyAEHandler";
  77.     switch (refcon)
  78.     {
  79.     case kAEOpenApplication:
  80.         error = MyGotRequiredParams (ae);
  81.         break;
  82.  
  83.     case kAEQuitApplication:
  84.         error = MyGotRequiredParams (ae);
  85.         if (error == 0)
  86.             doFileMenu(FILE_QUIT);
  87.         break;
  88.  
  89.     case kAEOpenDocuments:
  90.         ZERO(docList);
  91.         Clue1 = "\pAEGetParamDesc";
  92.         error = AEGetParamDesc(ae, keyDirectObject, typeAEList, &docList);
  93.         if (error == 0)
  94.             error = MyGotRequiredParams (ae);
  95.         if (error == 0)
  96.         {
  97.             Clue1 = "\pAECountItems";
  98.             error = AECountItems (&docList, &itemsInList);
  99.         }
  100.         if (error == 0)
  101.             if (itemsInList < 1)
  102.             {
  103.                 Clue1 = "\pitemsInList";
  104.                 error = errAEEventNotHandled;
  105.             }
  106.         for (index = 1; error == 0 && index <= itemsInList; index++)
  107.         {
  108.             if (error == 0)
  109.             {
  110.                 Clue1 = "\pAEGetNthPtr";
  111.                 error = AEGetNthPtr(&docList, index, typeFSS, &keywd, &returnedType,
  112.                         (Ptr)&myFSS, sizeof(myFSS), &actualSize);
  113.             }
  114.             if (error == 0)
  115.                 error = MyOpenFile(&myFSS);
  116.         }
  117.         (void) AEDisposeDesc(&docList);
  118.         break;
  119.  
  120.     default:
  121.         error = errAEEventNotHandled;
  122.     }
  123.     return error;
  124. }
  125.  
  126.  
  127.  
  128. /*
  129.  *=========================================================================
  130.  * DoHighLevelEvent (theEvent) - handle high level event
  131.  * entry:    theEvent = ptr to event
  132.  * returns:    OSErr
  133.  *=========================================================================
  134.  */
  135. OSErr
  136. DoHighLevelEvent (EventRecord *theEvent)
  137. {
  138.     OSErr        error;
  139.  
  140.     Clue1 = "\pAEProcessAppleEvent";
  141.     error = AEProcessAppleEvent (theEvent);
  142.     if (error)
  143.     {
  144.         ClueID = error;
  145.         panic (false, E_SYS, "\pDoHighLevelEvent", Clue1, nil);
  146.     }
  147.     return error;
  148. }
  149.  
  150.  
  151.  
  152. /*
  153.  *=========================================================================
  154.  * MyGotRequiredParams(ae) - check that all required params fetched
  155.  * entry:    ae = ptr to Apple event
  156.  * returns:    OSErr
  157.  *=========================================================================
  158.  */
  159. OSErr
  160. MyGotRequiredParams(AppleEvent *ae)
  161. {
  162.     OSErr        error;
  163.     DescType    returnedType;
  164.     Size        actualSize;
  165.  
  166.     Clue1 = "\pMyGotRequiredParams";
  167.     error = AEGetAttributePtr(ae, keyMissedKeywordAttr, typeWildCard,
  168.         &returnedType, nil, 0, &actualSize);
  169.     if (error == errAEDescNotFound)
  170.         return 0;
  171.     if (error == 0)
  172.         return errAEEventNotHandled;
  173.     return error;
  174. }
  175.  
  176.  
  177.  
  178. /*
  179.  *=========================================================================
  180.  * MyOpenFile(fss) - squirrel away file spec for later
  181.  *    (Called during startup processing)
  182.  * entry:    fss = ptr to file system spec
  183.  * returns:    OSErr
  184.  *            File_list[] possibly modified
  185.  *=========================================================================
  186.  */
  187. OSErr
  188. MyOpenFile(FSSpec *fss)
  189. {
  190.     OSErr        error;
  191.     file_info_t    *fi;
  192.     int            j;                /* File_list index */
  193.     StringHandle sh;            /* temp for file name */
  194.     FInfo        finfo;            /* finder info for file */
  195.     Str255        s;                /* temp for full path name */
  196.  
  197.     Clue1 = "\pFSpGetFInfo";
  198.     error = FSpGetFInfo(fss, &finfo);
  199.     if (error)
  200.         return error;
  201.     switch (finfo.fdType)
  202.     {
  203.     case TYPE_PREF:    j = FL_PREF; break;
  204.     case TYPE_CONT:    j = FL_DIST; break;
  205.     case 'INIT':    j = -1; Flags |= PF_STARTUP; break;
  206.     default:        j = -1; error = errAEEventNotHandled; break;
  207.     }
  208.     if (j >= 0)
  209.     {
  210.         fi = &File_list[j];
  211.         error = getInfo (fss->name, fss->vRefNum, fss->parID, &fi->f_info);
  212.         if (error == 0)
  213.         {
  214.             fi->f_vol = fss->vRefNum;
  215.             fi->f_launch = true;
  216.             fi->f_set = true;
  217.             sh = fi->f_path;
  218.             if (sh)
  219.                 DisposHandle ((Handle) sh);
  220.             sh = 0;
  221.             COPYPS (fss->name, s);
  222.             (void) fullpath (s, fss->vRefNum, fss->parID);
  223.             sh = (StringHandle) NewHandle ((Size) s[0] + 1);
  224.             if (sh)
  225.                 COPYPS (s, *sh);
  226.             else
  227.                 error = MemError ();
  228.             fi->f_path = sh;
  229.         }
  230.     }
  231.     return error;
  232. }
  233.